package com.cozy.android.sdk;

import com.cozy.android.sdk.ad.CozyVideoCallback;
import com.cozy.android.sdk.bridge.CozyUnityBridge;
import com.cozy.android.sdk.ad.CozyVideoManger;

import org.json.JSONObject;

public class CozyAdSdk {

    public static void showAd(String ad_source) {
        showAd(ad_source, "");
    }

    private static void adCallBack(String state, String ad_source) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ad_source", ad_source);
            CozyUnityBridge.getInstance().XSSdkCallback(state, jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void showAd(String ad_source, String q) {
        showAd(new CozyVideoCallback() {
            @Override
            public void onAdOver() {
                adCallBack("ad_over_cozy", ad_source);
            }

            @Override
            public void onAdError() {
                CozyUnityBridge.getInstance().XSSdkCallback("ad_error_cozy", "{}");
            }
        }, 0);
    }

    public static void showAd(CozyVideoCallback callback, int i) {
        try {
            CozyVideoManger.getReward1Instance().showVideo(CozyUnityBridge.mainGameActivity.get(), createVideoCallback(callback));
        } catch (Exception e) {
            e.printStackTrace();
            callbackAdError(callback);
        }
    }



    private static CozyVideoCallback createVideoCallback(final CozyVideoCallback callback) {
        return new CozyVideoCallback() {
            @Override
            public void onAdOver() {
                callbackOver(callback);
            }

            @Override
            public void onAdError() {
                callbackAdError(callback);
            }
        };
    }

    private static void callbackOver(CozyVideoCallback callback) {
        if (callback != null) {
            callback.onAdOver();
        }
    }

    private static void callbackAdError(CozyVideoCallback callback) {
        if (callback != null) {
            callback.onAdError();
        }
    }


}
